﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Common;
using EcSalseManage.App_Code;
using System.IO;
using System.Net;

namespace EcSalseManage.EcSalse
{
    public partial class Report_SalesLink_Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!CommonFunction.isInPrev("Report_Deal_0001"))
                {
                    Response.Redirect("../Error/Perm_Error.aspx");
                }
                if (!CommonFunction.isInPrev("Report_Deal_0002"))
                {
                    this.btnSubmit.Enabled = false;
                }
                if (!CommonFunction.isInPrev("Report_Deal_0003"))
                {
                    this.btnDelete.Enabled = false;
                }

                DataAccess.Operate.DA_Report_PropertyType_Inherit Report_PropertyType_Inherit = new DataAccess.Operate.DA_Report_PropertyType_Inherit();
                DataSet ds = Report_PropertyType_Inherit.FindLibrary();
                this.radlPropertyType.DataTextField = "Report_PropertyType_Name";
                this.radlPropertyType.DataValueField = "Report_PropertyType_ID";
                this.radlPropertyType.DataSource = ds.Tables[0];
                this.radlPropertyType.DataBind();

                DataAccess.Operate.DA_Report_PayType_Inherit Report_PayType_Inherit = new DataAccess.Operate.DA_Report_PayType_Inherit();
                ds = Report_PayType_Inherit.FindLibraryAll();
                this.dropReport_SalesLink_PayType.DataTextField = "Report_PayType_Name";
                this.dropReport_SalesLink_PayType.DataValueField = "Report_PayType_ID";
                this.dropReport_SalesLink_PayType.DataSource = ds.Tables[0];
                this.dropReport_SalesLink_PayType.DataBind();

                DataAccess.Operate.DA_Report_Attach_Inherit Report_Attach_Inherit = new DataAccess.Operate.DA_Report_Attach_Inherit();
                ds = Report_Attach_Inherit.FindLibrary();
                this.ddlFileType.DataSource = ds.Tables[0];
                this.ddlFileType.DataTextField = "Report_File_Name";
                this.ddlFileType.DataValueField = "Report_File_ID";
                this.ddlFileType.DataBind();

                if (Request.QueryString["id"] != null)
                {
                    ViewState["id"] = Request.QueryString["id"].ToString();
                    Update_Data_Bind(ViewState["id"].ToString());
                    BindGrid();
                    this.btnReset.Visible = false;
                    this.btnDelete.Visible = true;
                    this.Report_Tab1.Visible = true;
                    this.Report_Tab1.ExchangeTypeID = "3";
                    this.Report_Tab1.MainID = ViewState["id"].ToString();
                }
                else
                {
                    DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
                    this.lblProjectName.Text = Project_Inherit.getProjectName(Session["ProjectID"].ToString());
                    ViewState["id"] = null;
                    this.btnAudit.Visible = false;
                }
            }
        }

        #region 提交操作
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Guid gReport = Guid.NewGuid();
            DataAccess.Operate.DA_Report_SalesLink_Operate Report_SalesLink_Operate = new DataAccess.Operate.DA_Report_SalesLink_Operate();
            DataEntity.T_Report_SalesLink Report_SalesLink = new DataEntity.T_Report_SalesLink();
            Report_SalesLink.Report_SalesLink_ID = gReport;
            Report_SalesLink.Report_Plan_Audit = 1;
            Report_SalesLink.Report_SalesLink_AgencyCost = Convert.ToDecimal(txtReport_SalesLink_AgencyCost.Text.Trim());
            Report_SalesLink.Report_SalesLink_ExchangeTypeID = 3;
            Report_SalesLink.Report_SalesLink_SellUnit = txtReport_SalesLink_SellUnit.Text.Trim();
            Report_SalesLink.Report_SalesLink_PropertyTypeID = Convert.ToInt32(this.radlPropertyType.SelectedValue);
            Report_SalesLink.Report_SalesLink_ExchangeDate = Convert.ToDateTime(txtReport_SalesLink_ExchangeDate.Text.Trim());
            Report_SalesLink.Report_SalesLink_ReportNO = txtReport_SalesLink_ReportNO.Text.Trim();
            Report_SalesLink.Report_SalesLink_Subscribe = txtReport_SalesLink_Subscribe.Text.Trim();
            Report_SalesLink.Report_SalesLink_Operator = txtReport_SalesLink_Operator.Text.Trim();
            Report_SalesLink.Report_SalesLink_Operator = txtReport_SalesLink_Operator.Text.Trim();
            Report_SalesLink.Report_SalesLink_Buyer = txtReport_SalesLink_Buyer.Text.Trim();
            Report_SalesLink.Report_SalesLink_Area = Convert.ToDecimal(txtReport_SalesLink_Area.Text.Trim());
            Report_SalesLink.Report_SalesLink_Discount = txtReport_SalesLink_Discount.Text.Trim();
            Report_SalesLink.Report_SalesLink_PayType = dropReport_SalesLink_PayType.SelectedValue;
            Report_SalesLink.Report_SalesLink_Price = Convert.ToDecimal(txtReport_SalesLink_Price.Text.Trim());
            Report_SalesLink.Report_SalesLink_Margins = Convert.ToDecimal(txtReport_SalesLink_Margins.Text.Trim());
            Report_SalesLink.Report_SalesLink_AgencyCost = Convert.ToDecimal(txtReport_SalesLink_AgencyCost.Text.Trim());
            Report_SalesLink.Report_SalesLink_CancelComm = Convert.ToDecimal(txtReport_SalesLink_CancelComm.Text.Trim());
            Report_SalesLink.Report_SalesLink_Variations = txtReport_SalesLink_Variations.Text.Trim();
            Report_SalesLink.Report_SalesLink_IDNumber = txtReport_SalesLink_IDNumber.Text.Trim();
            Report_SalesLink.Report_SalesLink_Tel = txtReport_SalesLink_Tel.Text.Trim();
            Report_SalesLink.Report_SalesLink_CustomerAddress = txtReport_SalesLink_CustomerAddress.Text.Trim();
            Report_SalesLink.Report_SalesLink_PostCode = txtReport_SalesLink_PostCode.Text.Trim();
            Report_SalesLink.Report_SalesLink_SalesArea = Convert.ToDecimal(txtReport_SalesLink_SalesArea.Text.Trim());
            Report_SalesLink.Report_SalesLink_OriginalContractPrice = Convert.ToDecimal(txtReport_SalesLink_OriginalContractPrice.Text.Trim());
            Report_SalesLink.Report_SalesLink_BigDate = Convert.ToDateTime(txtReport_SalesLink_BigDate.Text.Trim());
            Report_SalesLink.Report_SalesLink_BigPrice = Convert.ToDecimal(txtReport_SalesLink_BigPrice.Text.Trim());
            Report_SalesLink.Report_SalesLink_BigSumPrice = Convert.ToDecimal(txtReport_SalesLink_BigSumPrice.Text.Trim());
            Report_SalesLink.Report_SalesLink_ContractDate = Convert.ToDateTime(txtReport_SalesLink_ContractDate.Text.Trim());
            Report_SalesLink.Report_SalesLink_ContractArea = Convert.ToDecimal(txtReport_SalesLink_ContractArea.Text.Trim());
            Report_SalesLink.Report_SalesLink_ContractPrice = Convert.ToDecimal(txtReport_SalesLink_ContractPrice.Text.Trim());
            Report_SalesLink.Report_SalesLink_ContractSumPrice = Convert.ToDecimal(txtReport_SalesLink_ContractSumPrice.Text.Trim());
            Report_SalesLink.Report_SalesLink_LoanAmount = Convert.ToDecimal(txtReport_SalesLink_LoanAmount.Text.Trim());
            Report_SalesLink.Report_SalesLink_ContractLoansDate = Convert.ToDateTime(txtReport_SalesLink_ContractLoansDate.Text.Trim());
            Report_SalesLink.Report_SalesLink_LentDate = Convert.ToDateTime(txtReport_SalesLink_LentDate.Text.Trim());
            Report_SalesLink.Report_SSalesLink_LentAmount = Convert.ToDecimal(txtReport_SalesLink_LentAmount.Text.Trim());
            Report_SalesLink.Report_SalesLink_TotalReceipt = Convert.ToDecimal(txtReport_SalesLink_TotalReceipt.Text.Trim());
            Report_SalesLink.Report_SalesLink_RestHouse = Convert.ToDecimal(txtReport_SalesLink_RestHouse.Text.Trim());
            Report_SalesLink.Report_SalesLink_GiveHouseDate = Convert.ToDateTime(txtReport_SalesLink_GiveHouseDate.Text.Trim());
            Report_SalesLink.Report_SalesLink_OriginalArea =Convert.ToDecimal(txtReport_SalesLink_OriginalArea.Text.Trim());
            Report_SalesLink.Report_SalesLink_OriginalPrice = Convert.ToDecimal(txtReport_SalesLink_OriginalPrice.Text.Trim());
            Report_SalesLink.Report_SalesLink_BarchSalse = txtReport_SalesLink_BarchSalse.Text.Trim();
            Report_SalesLink.Report_SalesLink_Departments=txtReport_SalesLink_Departments.Text.Trim();
            Report_SalesLink.Report_SalesLink_Branches=txtReport_SalesLink_Branches.Text.Trim();
            Report_SalesLink.Report_SalesLink_SalesNames=txtReport_SalesLink_SalesNames.Text.Trim();
            Report_SalesLink.Report_SalesLink_SalesNumber = txtReport_SalesLink_SalesNumber.Text.Trim();

            if (ViewState["id"] == null)
            {
                Report_SalesLink.Report_SalesLink_ProjectID = new Guid(Session["ProjectID"].ToString());
                if (Report_SalesLink_Operate.Insert(Report_SalesLink))
                {
                    if (this.hfldActualCollection.Value != "")
                    {
                        DataAccess.Operate.DA_Report_ActualCollection_Operate Report_ActualCollection_Operate = new DataAccess.Operate.DA_Report_ActualCollection_Operate();
                        DataEntity.T_Report_ActualCollection Report_ActualCollection = new DataEntity.T_Report_ActualCollection();
                        string[] item = this.hfldActualCollection.Value.Substring(0, this.hfldActualCollection.Value.Length - 1).Split('|');
                        for (int i = 0; i < item.Length; i++)
                        {
                            string[] entity = item[i].Split(',');
                            Report_ActualCollection.Report_ActualCollection_MainID = gReport;
                            Report_ActualCollection.Report_ActualCollection_Amount = Convert.ToDecimal(entity[0]);
                            Report_ActualCollection.Report_ActualCollection_Date = Convert.ToDateTime(entity[1]);
                            Report_ActualCollection_Operate.Insert(Report_ActualCollection);
                        }
                    }
                    if (this.hfldContractInstallments.Value != "")
                    {
                        DataAccess.Operate.DA_Report_ContractInstallments_Operate Report_ContractInstallments_Operate = new DataAccess.Operate.DA_Report_ContractInstallments_Operate();
                        DataEntity.T_Report_ContractInstallments Report_ContractInstallments = new DataEntity.T_Report_ContractInstallments();
                        string[] item = this.hfldContractInstallments.Value.Substring(0, this.hfldContractInstallments.Value.Length - 1).Split('|');
                        for (int i = 0; i < item.Length; i++)
                        {
                            string[] entity = item[i].Split(',');
                            Report_ContractInstallments.Report_ContractInstallments_MainID = gReport;
                            Report_ContractInstallments.Report_ContractInstallments_Amount = Convert.ToDecimal(entity[0]);
                            Report_ContractInstallments.Report_ContractInstallments_Date = Convert.ToDateTime(entity[1]);
                            Report_ContractInstallments_Operate.Insert(Report_ContractInstallments);
                        }
                    }
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 1, gReport, 1);

                    #region 上传附件部分
                    DataAccess.Operate.DA_Report_Attach_Operate Report_Attach_Operate = new DataAccess.Operate.DA_Report_Attach_Operate();
                    string strFiles = this.HiddenField1.Value.Substring(0, this.HiddenField1.Value.Length - 1);
                    string delimStr = "|";
                    char[] delimiter = delimStr.ToCharArray();
                    string[] split = strFiles.Split(delimiter, 4);
                    for (int i = 0; i < split.Length; i++)
                    {
                        DataEntity.T_Report_Attach Report_Attach = new DataEntity.T_Report_Attach();
                        Report_Attach.Project_Report_ID = Guid.NewGuid();
                        Report_Attach.Project_Report_MainID = gReport;
                        delimStr = ",";
                        delimiter = delimStr.ToCharArray();
                        string[] item = split[i].ToString().Split(delimiter, 3);
                        string sFileName = "";
                        string sFilePath = "";
                        string sFileType = "";
                        if (!Directory.Exists("D:\\ReportManager\\" + DateTime.Now.Year.ToString()))
                        {
                            Directory.CreateDirectory("D:\\ReportManager\\" + DateTime.Now.Year.ToString());
                        }
                        if (!Directory.Exists("D:\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + gReport.ToString()))
                        {
                            Directory.CreateDirectory("D:\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + gReport.ToString());
                        }
                        int fileType = item[1].LastIndexOf('.');
                        sFileType = item[1].Substring(fileType);
                        sFileName = gReport.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + sFileType;
                        sFilePath = "\\ReportManager\\" + DateTime.Now.Year + "\\" + gReport.ToString() + "\\" + sFileName;
                        string uriString = "D:\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + gReport.ToString() + "\\" + sFileName;

                        UpLoadFile(item[1], uriString);

                        Report_Attach.Project_Report_FileID = Convert.ToInt32(item[0]);
                        Report_Attach.Project_Report_ExchangeTypeID = 4;
                        Report_Attach.Project_Report_FilePath = sFilePath;
                        Report_Attach.Project_Report_FileDesc = item[2];
                        Report_Attach.Project_Report_FileName = sFileName;
                        Report_Attach_Operate.Insert(Report_Attach);

                        DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 5, gReport, 4);
                    }
                    #endregion

                    Response.Write("<script>alert('" + Common_Const.MSG_ALL_INSERT_SUCCESS + "');</script>");
                    SendMessage("10", Session["ProjectID"].ToString());
                }
                else
                {
                    Response.Write("<script>alert('" + Common_Const.MSG_ALL_INSERT_FAILS + "');</script>");
                }
            }
            else
            {
                Report_SalesLink.Report_SalesLink_ID = new Guid(ViewState["id"].ToString());
                if (Report_SalesLink_Operate.Update(Report_SalesLink))
                {
                    if (this.hfldActualCollection.Value != "")
                    {
                        DataAccess.Operate.DA_Report_ActualCollection_Operate Report_ActualCollection_Operate = new DataAccess.Operate.DA_Report_ActualCollection_Operate();
                        DataAccess.Operate.DA_Report_ActualCollection_Inherit Report_ActualCollection_Inherit = new DataAccess.Operate.DA_Report_ActualCollection_Inherit();
                        Report_ActualCollection_Inherit.DeleteByMainID(ViewState["id"].ToString());
                        DataEntity.T_Report_ActualCollection Report_ActualCollection = new DataEntity.T_Report_ActualCollection();
                        string[] item = this.hfldActualCollection.Value.Substring(0, this.hfldActualCollection.Value.Length - 1).Split('|');
                        for (int i = 0; i < item.Length; i++)
                        {
                            string[] entity = item[i].Split(',');
                            Report_ActualCollection.Report_ActualCollection_MainID = new Guid(ViewState["id"].ToString());
                            Report_ActualCollection.Report_ActualCollection_Amount = Convert.ToDecimal(entity[0]);
                            Report_ActualCollection.Report_ActualCollection_Date = Convert.ToDateTime(entity[1]);
                            Report_ActualCollection_Operate.Insert(Report_ActualCollection);
                        }
                    }
                    if (this.hfldContractInstallments.Value != "")
                    {
                        DataAccess.Operate.DA_Report_ContractInstallments_Operate Report_ContractInstallments_Operate = new DataAccess.Operate.DA_Report_ContractInstallments_Operate();
                        DataAccess.Operate.DA_Report_ContractInstallments_Inherit Report_ContractInstallments_Inherit = new DataAccess.Operate.DA_Report_ContractInstallments_Inherit();
                        Report_ContractInstallments_Inherit.DeleteByMainID(ViewState["id"].ToString());
                        DataEntity.T_Report_ContractInstallments Report_ContractInstallments = new DataEntity.T_Report_ContractInstallments();
                        string[] item = this.hfldContractInstallments.Value.Substring(0, this.hfldContractInstallments.Value.Length - 1).Split('|');
                        for (int i = 0; i < item.Length; i++)
                        {
                            string[] entity = item[i].Split(',');
                            Report_ContractInstallments.Report_ContractInstallments_MainID = new Guid(ViewState["id"].ToString());
                            Report_ContractInstallments.Report_ContractInstallments_Amount = Convert.ToDecimal(entity[0]);
                            Report_ContractInstallments.Report_ContractInstallments_Date = Convert.ToDateTime(entity[1]);
                            Report_ContractInstallments_Operate.Insert(Report_ContractInstallments);
                        }
                    }
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 1, gReport,2);
                    Response.Write("<script>alert('" + Common_Const.MSG_ALL_UPDATE_SUCCESS + "');</script>");
                }
                else
                {
                    Response.Write("<script>alert('" + Common_Const.MSG_ALL_UPDATE_FAILS + "');</script>");
                }
            }

        }
        #endregion

        #region 修改时显示
        private void Update_Data_Bind(string mainID)
        {
            DataAccess.Operate.DA_Report_SalesLink_Operate Report_SalesLink_Operate = new DataAccess.Operate.DA_Report_SalesLink_Operate();
            DataEntity.T_Report_SalesLink Report_SalesLink = (DataEntity.T_Report_SalesLink)Report_SalesLink_Operate.SelectOne(mainID);
            txtReport_SalesLink_AgencyCost.Text = Report_SalesLink.Report_SalesLink_AgencyCost.ToString();
            this.radlPropertyType.SelectedValue = Report_SalesLink.Report_SalesLink_PropertyTypeID.ToString();
            txtReport_SalesLink_ExchangeDate.Text = Report_SalesLink.Report_SalesLink_ExchangeDate.ToString();
            txtReport_SalesLink_ReportNO.Text = Report_SalesLink.Report_SalesLink_ReportNO.ToString();
            txtReport_SalesLink_Subscribe.Text = Report_SalesLink.Report_SalesLink_Subscribe.ToString();
            txtReport_SalesLink_Operator.Text = Report_SalesLink.Report_SalesLink_Operator.ToString();
            txtReport_SalesLink_Operator.Text = Report_SalesLink.Report_SalesLink_Operator.ToString();
            txtReport_SalesLink_Buyer.Text = Report_SalesLink.Report_SalesLink_Buyer.ToString();
            txtReport_SalesLink_SellUnit.Text = Report_SalesLink.Report_SalesLink_SellUnit.ToString();
            txtReport_SalesLink_Area.Text = Report_SalesLink.Report_SalesLink_Area.ToString();
            txtReport_SalesLink_Discount.Text = Report_SalesLink.Report_SalesLink_Discount.ToString();
            dropReport_SalesLink_PayType.SelectedValue = Report_SalesLink.Report_SalesLink_PayType.ToString();
            txtReport_SalesLink_Price.Text = Report_SalesLink.Report_SalesLink_Price.ToString();
            txtReport_SalesLink_Margins.Text = Report_SalesLink.Report_SalesLink_Margins.ToString();
            txtReport_SalesLink_AgencyCost.Text = Report_SalesLink.Report_SalesLink_AgencyCost.ToString();
            txtReport_SalesLink_CancelComm.Text = Report_SalesLink.Report_SalesLink_CancelComm.ToString();
            txtReport_SalesLink_Variations.Text = Report_SalesLink.Report_SalesLink_Variations.ToString();
            txtReport_SalesLink_IDNumber.Text = Report_SalesLink.Report_SalesLink_IDNumber.ToString();
            txtReport_SalesLink_Tel.Text = Report_SalesLink.Report_SalesLink_Tel.ToString();
            txtReport_SalesLink_CustomerAddress.Text = Report_SalesLink.Report_SalesLink_CustomerAddress.ToString();
            txtReport_SalesLink_PostCode.Text = Report_SalesLink.Report_SalesLink_PostCode.ToString();
            txtReport_SalesLink_SalesArea.Text = Report_SalesLink.Report_SalesLink_SalesArea.ToString();
            txtReport_SalesLink_OriginalContractPrice.Text = Report_SalesLink.Report_SalesLink_OriginalContractPrice.ToString();
            txtReport_SalesLink_BigDate.Text = Report_SalesLink.Report_SalesLink_BigDate.ToString();
            txtReport_SalesLink_BigPrice.Text = Report_SalesLink.Report_SalesLink_BigPrice.ToString();
            txtReport_SalesLink_BigSumPrice.Text = Report_SalesLink.Report_SalesLink_BigSumPrice.ToString();
            txtReport_SalesLink_ContractDate.Text = Report_SalesLink.Report_SalesLink_ContractDate.ToString();
            txtReport_SalesLink_ContractArea.Text = Report_SalesLink.Report_SalesLink_ContractArea.ToString();
            txtReport_SalesLink_ContractPrice.Text = Report_SalesLink.Report_SalesLink_ContractPrice.ToString();
            txtReport_SalesLink_ContractSumPrice.Text = Report_SalesLink.Report_SalesLink_ContractSumPrice.ToString();
            txtReport_SalesLink_LoanAmount.Text = Report_SalesLink.Report_SalesLink_LoanAmount.ToString();
            txtReport_SalesLink_ContractLoansDate.Text = Report_SalesLink.Report_SalesLink_ContractLoansDate.ToString();
            txtReport_SalesLink_LentDate.Text = Report_SalesLink.Report_SalesLink_LentDate.ToString();
            txtReport_SalesLink_LentAmount.Text = Report_SalesLink.Report_SSalesLink_LentAmount.ToString();
            txtReport_SalesLink_TotalReceipt.Text = Report_SalesLink.Report_SalesLink_TotalReceipt.ToString();
            txtReport_SalesLink_RestHouse.Text = Report_SalesLink.Report_SalesLink_RestHouse.ToString();
            txtReport_SalesLink_GiveHouseDate.Text = Report_SalesLink.Report_SalesLink_GiveHouseDate.ToString();
            txtReport_SalesLink_OriginalArea.Text = Report_SalesLink.Report_SalesLink_OriginalArea.ToString();
            txtReport_SalesLink_OriginalPrice.Text = Report_SalesLink.Report_SalesLink_OriginalPrice.ToString();
            txtReport_SalesLink_BarchSalse.Text = Report_SalesLink.Report_SalesLink_BarchSalse.ToString();
            txtReport_SalesLink_Departments.Text = Report_SalesLink.Report_SalesLink_Departments.ToString();
            txtReport_SalesLink_Branches.Text = Report_SalesLink.Report_SalesLink_Branches.ToString();
            txtReport_SalesLink_SalesNames.Text = Report_SalesLink.Report_SalesLink_SalesNames.ToString();
            txtReport_SalesLink_SalesNumber.Text = Report_SalesLink.Report_SalesLink_SalesNumber.ToString();
          

            DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
            this.lblProjectName.Text = Project_Inherit.getProjectName(Report_SalesLink.Report_SalesLink_ProjectID.ToString());
            ViewState["ProjectID"] = Report_SalesLink.Report_SalesLink_ProjectID.ToString();

            if (Report_SalesLink.Report_Plan_Audit > 8)
            {
                this.btnDelete.Enabled = false;
                this.btnSubmit.Enabled = false;
            }
            if (Report_SalesLink.Report_Plan_Audit.ToString() == "8")
                this.btnAudit.Text = "项经审核";
            if (Report_SalesLink.Report_Plan_Audit.ToString() == "10")
                this.btnAudit.Text = "营销[副]总监审核";
            if (Report_SalesLink.Report_Plan_Audit.ToString() == "11")
            {
                this.btnAudit.Text = "审核已完成";
                this.btnAudit.Enabled = false;
            }

            if (!CommonFunction.isInPrev("Report_Deal_0005") && this.btnAudit.Text == "项经审核")
            {
                this.btnAudit.Enabled = false;
            }
            if (!CommonFunction.isInPrev("Report_Deal_0006") && this.btnAudit.Text == "营销[副]总监审核")
            {
                this.btnAudit.Enabled = false;
            }

            DataSet ds = new DataSet();
            string shtml = "";
            DataAccess.Operate.DA_Report_ContractInstallments_Inherit Report_ContractInstallments_Inherit = new DataAccess.Operate.DA_Report_ContractInstallments_Inherit();
            ds = Report_ContractInstallments_Inherit.FindByMainID(mainID);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (i == 0)
                    shtml = "document.getElementById('ContractInstallments1').value='" + ds.Tables[0].Rows[0]["Report_ContractInstallments_Amount"] + "';document.getElementById('ContractInstallments2').value='" + ds.Tables[0].Rows[0]["Report_ContractInstallments_Date"] + "';";
                else
                    shtml = "var div=document.createElement('div');div.innerHTML='分期付款（金额）<input type=\"text\"  name=\"ContractInstallments1\" style=\"width:50px;\" value=\"" + ds.Tables[0].Rows[i]["Report_ContractInstallments_Amount"] + "\"  />&nbsp;&nbsp;&nbsp;&nbsp; 约定日期<input type=\"text\" name=\"ContractInstallments2\"  value=\"" + ds.Tables[0].Rows[i]["Report_ContractInstallments_Date"] + "\" /> <input type=\"button\" value=\"删除\" onclick=\"DelArea(&quot;ContractInstallmentsArea&quot;,this.parentNode)\">';document.getElementById('ContractInstallmentsArea').appendChild(div);";
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "ContractInstallments" + i, "<script>" + shtml + "</script> ");
            }

            DataAccess.Operate.DA_Report_ActualCollection_Inherit Report_ActualCollection_Inherit = new DataAccess.Operate.DA_Report_ActualCollection_Inherit();
            ds = Report_ActualCollection_Inherit.FindByMainID(mainID);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (i == 0)
                    shtml = "document.getElementById('ActualCollection1').value='" + ds.Tables[0].Rows[0]["Report_ActualCollection_Amount"] + "';document.getElementById('ActualCollection2').value='" + ds.Tables[0].Rows[0]["Report_ActualCollection_Date"] + "';";
                else
                    shtml = "var div=document.createElement('div');div.innerHTML='实付款（金额）<input type=\"text\"  style=\"width:50px;\" name=\"ActualCollection1\" value=\"" + ds.Tables[0].Rows[i]["Report_ActualCollection_Amount"] + "\"  /> &nbsp;&nbsp;&nbsp;&nbsp;日期<input type=\"text\" name=\"ActualCollection2\"  value=\"" + ds.Tables[0].Rows[i]["Report_ActualCollection_Date"] + "\" /> <input type=\"button\" value=\"删除\" onclick=\"DelArea(&quot;ActualCollectionArea&quot;,this.parentNode)\">';document.getElementById('ActualCollectionArea').appendChild(div);";
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "ActualCollection" + i, "<script>" + shtml + "</script> ");
            }

        }
        #endregion

        protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            DataAccess.Operate.DA_Report_Attach_Operate Report_Attach_Operate = new DataAccess.Operate.DA_Report_Attach_Operate();
            DataEntity.T_Report_Attach Report_Attach = new DataEntity.T_Report_Attach();
            string sFileName = "";
            string sFilePath = "";
            string sFileType = "";

            if (!Directory.Exists("D:\\ReportManager\\" + DateTime.Now.Year.ToString()))
            {
                Directory.CreateDirectory("D:\\ReportManager\\" + DateTime.Now.Year.ToString());
            }
            if (!Directory.Exists("D:\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + ViewState["id"].ToString()))
            {
                Directory.CreateDirectory("D:\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + ViewState["id"].ToString());
            }

            sFileType = CommonFunction.CheckFileType(this.txtFilePath.PostedFile.FileName);
            if (sFileType != "")
            {
                sFileName = ViewState["id"].ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + sFileType;
            }

            sFilePath = "\\ReportManager\\" + DateTime.Now.Year + "\\" + ViewState["id"].ToString() + "\\" + sFileName;
            try
            {
                Report_Attach.Project_Report_FileDesc = this.txtProjectDesc.Text.Trim();
                Report_Attach.Project_Report_FileName = sFileName;
                Report_Attach.Project_Report_FilePath = sFilePath;
                Report_Attach.Project_Report_ExchangeTypeID = Convert.ToInt32(this.Report_Tab1.ExchangeTypeID);
                Report_Attach.Project_Report_FileID = Convert.ToInt32(this.ddlFileType.SelectedValue);
                Report_Attach.Project_Report_MainID = new Guid(ViewState["id"].ToString());
                Report_Attach_Operate.Insert(Report_Attach);
                this.txtFilePath.PostedFile.SaveAs("D:\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + ViewState["id"].ToString() + "\\" + sFileName);
                DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 5, new Guid(ViewState["id"].ToString()), 4);
                BindGrid();
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {

            }
        }

        #region BindGrid
        private void BindGrid()
        {
            DataSet ds = new DataSet();
            DataAccess.Operate.DA_Report_Attach_Inherit Report_Attach_Inherit = new DataAccess.Operate.DA_Report_Attach_Inherit();

            dgList.DataBind();

            ds = Report_Attach_Inherit.GetAllByReportID(ViewState["id"].ToString());
            dgList.DataSource = ds;
            dgList.DataBind();
        }
        #endregion

        protected void dgList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (e.Item.ItemIndex > -1)
                {
                    ImageButton imgbtnDelete = (ImageButton)e.Item.FindControl("imgbtnDelete");
                    imgbtnDelete.Attributes.Add("onclick", "return window.confirm(\"是否确定删除？\")");

                }
            }
        }

        protected void dgList_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            DataAccess.Operate.DA_Report_Attach_Operate Report_Attach_Operate = new DataAccess.Operate.DA_Report_Attach_Operate();
            if (e.CommandName.ToString() == "Delete")
            {
                try
                {
                    Report_Attach_Operate.Delete(new Guid(dgList.DataKeys[e.Item.ItemIndex].ToString()));
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 5, new Guid(ViewState["id"].ToString()), 3);
                    this.BindGrid();
                }
                catch (System.Exception ex)
                {
                    throw new Exception(ex.Message.ToString());
                }
                finally
                {
                    Report_Attach_Operate = null;
                }
            }
        }

        #region WebClient上传附件
        /// <summary>
        /// WebClient上传文件至服务器
        /// </summary>
        /// <param name="fileNamePath">文件名，全路径格式</param>
        /// <param name="uriString">服务器文件夹路径</param>
        public void UpLoadFile(string fileNamePath, string uriString)
        {
            /**/
            /// 创建WebClient实例 
            WebClient myWebClient = new WebClient();
            myWebClient.Credentials = CredentialCache.DefaultCredentials;
            // 要上传的文件 
            FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
            // FileStream fs = OpenFile(); 
            BinaryReader r = new BinaryReader(fs);
            byte[] postArray = r.ReadBytes((int)fs.Length);
            Stream postStream = myWebClient.OpenWrite(uriString, " PUT ");
            try
            {

                // 使用UploadFile方法可以用下面的格式
                // myWebClient.UploadFile(uriString,"PUT",fileNamePath); 


                if (postStream.CanWrite)
                {
                    postStream.Write(postArray, 0, postArray.Length);
                    postStream.Close();
                    fs.Dispose();
                }
                else
                {
                    postStream.Close();
                    fs.Dispose();

                }
            }
            catch (Exception err)
            {
                postStream.Close();
                fs.Dispose();
                // Utility.LogWriter log = new Utility.LogWriter(); 
                throw err;
            }
            finally
            {
                postStream.Close();
                fs.Dispose();
            }
        }
        #endregion

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DataAccess.Operate.DA_Report_SalesLink_Operate Report_SalesLink_Operate = new DataAccess.Operate.DA_Report_SalesLink_Operate();
            if (Report_SalesLink_Operate.Delete(ViewState["id"].ToString()))
            {
                DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 1,new Guid(ViewState["id"].ToString()), 2);
                Response.Write("<script>alert('" + Common_Const.MSG_ALL_DELETE_SUCCESS + "');window.close();</script>");
            }
            else
            {
                Response.Write("<script>alert('" + Common_Const.MSG_ALL_DELETE_FAILS + "');</script>");
            }
        }

        protected void btnAudit_Click(object sender, EventArgs e)
        {
            if (this.btnAudit.Text == "项经审核")
            {
              
                DataAccess.Operate.DA_Report_SalesLink_Inherit Report_SalesLink_Inherit = new DataAccess.Operate.DA_Report_SalesLink_Inherit();
                if (Report_SalesLink_Inherit.AuditByID(ViewState["id"].ToString(), "10"))
                {
                   
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 6, new Guid(ViewState["id"].ToString()), 10);
                    Response.Write("<script>alert('审核成功');window.close();</script>");
                    SendMessage("5", ViewState["ProjectID"].ToString());   //通知营销[副]总监审批
                 
               
                }
            }
            else if (this.btnAudit.Text == "营销[副]总监审核")
            {
            
                DataAccess.Operate.DA_Report_SalesLink_Inherit Report_SalesLink_Inherit = new DataAccess.Operate.DA_Report_SalesLink_Inherit();
                if (Report_SalesLink_Inherit.AuditByID(ViewState["id"].ToString(), "11"))
                {
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 6, new Guid(ViewState["id"].ToString()), 5);
                    Response.Write("<script>alert('审核成功');window.close();</script>");
                }
            }
        }

        public void SendMessage(string operate, string project)
        {
            string projectNo = "";
            DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
            string[] array = Project_Inherit.getCodeAndName(out projectNo,operate, project);
            string sTitle = "请注意，项目编号:" + projectNo + "中的联动成交报告需要您的审批。";
            string sBody = "请注意，项目编号:" + projectNo + " 中的联动成交报告需要您的审批。<a href='http://wap.gzcentaline.com.cn'>点击此处</a>";
            // array[1] 姓名 ，array[0] 工号
            EcSalseManage.App_Code.CommonFunction.SendMessage(array[1], array[0], sTitle, sBody);

        }

    }
}
